package com.ququ.syb.balance.controller;

import com.ququ.common.result.Result;
import com.ququ.syb.balance.redis.RedisDao;
import com.ququ.syb.balance.service.AccountChannelBalanceBusinessService;
import com.ququ.syb.balance.service.AccountChannelbalanceService;
import com.ququ.syb.common.constant.RedisKeyConstant;
import com.ququ.syb.common.pojo.AccountChannelBalance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.Jedis;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class AccountChannelBalanceController {
    @Autowired
    private AccountChannelbalanceService accountChannelbalanceService;
    @Autowired
    private AccountChannelBalanceBusinessService accountChannelBalanceBusinessService;
    @Autowired
    protected RedisDao redisDao;

    private Map<String,String> shareBalanceMap = new HashMap<>();

    public void setShareBalanceMap() {
        Jedis jedis = redisDao.getJedis();
        this.shareBalanceMap = jedis.hgetAll(RedisKeyConstant.SHARE_BALANCE);
        redisDao.close(jedis);
    }

    @RequestMapping("/balance/channel")
    public Result<AccountChannelBalance> getAccountChannelBalance(@RequestParam String accountid,
                                                                  @RequestParam String channelid){
        if (shareBalanceMap.isEmpty())
            setShareBalanceMap();
        String realChannelid = shareBalanceMap.getOrDefault(channelid,channelid);
        return new Result<>(accountChannelbalanceService.getAccountChannelBalance(accountid, realChannelid));
    }

    @RequestMapping("/balance/all")
    public Result<List<AccountChannelBalance>> listAccountChannelBalance(@RequestParam String accountid){
        return new Result<>(accountChannelbalanceService.listAccountChannelBalance(accountid));
    }

    @RequestMapping("/balance/add")
    public Result<AccountChannelBalance> addBalance(@RequestParam String accountid,
                                                    @RequestParam String channelid,
                                                    @RequestParam Long amount,
                                                    @RequestParam Integer ordertype,
                                                    @RequestParam String tradenum,
                                                    @RequestParam Integer flowtype,
                                                    @RequestParam String appcode,
                                                    @RequestParam String remark){
        if (shareBalanceMap.isEmpty())
            setShareBalanceMap();
        String realChannelid = shareBalanceMap.getOrDefault(channelid,channelid);
        AccountChannelBalance accountChannelBalance = accountChannelBalanceBusinessService.addBalance(accountid, realChannelid, amount, ordertype, tradenum, flowtype, appcode, remark);
        if (accountChannelBalance == null)
            return new Result<>(-1,"重复入账错误");
        else
            return new Result<>(accountChannelBalance);
    }
}
